﻿using Yxinmt.Interface;
using Yxinmt.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace Yxinmt.Windows
{
    public class AlertConfrim : BaseWindow, IWindow
    {
        public AlertConfrim(System.Windows.Window window, IWindow parentWindow, bool isDialog, Dictionary<string,object> parameters)
        {
            Window = window;
            ParentWindow = parentWindow;
            _IsDialog = isDialog;
            IsDialogWindow = true;
            _Parameters = parameters;

            InitSet(string.Empty);
        }

        public void Alert(string message, bool blDialogBack = true)
        {
            Utility.Common.SetProperty(_View, "Cancel", "Visible", false);
            Utility.Common.SetProperty(_View, "BorderCenterLine", "Visible", false);

            ShowMessage(message, "提示信息", blDialogBack);
        }

        public void Confrim(string message, bool blDialogBack = true)
        {
            ShowMessage(message, "确认信息", blDialogBack);
        }

        public void ShowMessage(string message, string title, bool blDialogBack)
        {
            Utility.Common.SetProperty(_View, "Message", "Text", message);
            Utility.Common.SetProperty(_View, "Title", "Text", title);

            double height = Utility.Common.GetStringHeight(message, 540, 30);

            double h = 0;
            double w = 0;
            if (height > 140 && height < 260) h = height;
            else if (height >= 260 && height < 400) { h = 300; w = 750; }
            else if (height >= 400) { h = 450; w = 850; };

            Dictionary<string, object> dict = new Dictionary<string, object>();
            if (h > 0) { h += 220; dict.Add("Height", h); }
            if (w > 0) dict.Add("Width", w);

            if (dict.Count > 0) Utility.Common.SetProperty(_View, "BorderPanel", dict);

            if (blDialogBack)
            {
                dict = new Dictionary<string, object>();
                dict.Add("BackColor", _View["DialogBackColor"]);
                dict.Add("Opacity", _View["DialogOpacity"]);
                Utility.Common.SetProperty(_View, "BgDockPanel", dict);
            }

            this.SetWindow();

            this.GetElements().ForEach(c => this.Grid.Children.Add(c));
            this.EventLoad();
            this.DataLoadAsync();
        }

        public override void EventInvoke(object sender, EventArgs e, IControl control)
        {
            if (control.Name == "Ok") Window.DialogResult = true;
            else if (control.Name == "Cancel") Window.DialogResult = false;
        }
    }
}
